#include "tscriptdelegate.h"
#include <QTableWidgetItem>
#include <QTextEdit>

tScriptDelegate::tScriptDelegate()
{

}

QWidget *tScriptDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QTextEdit *editor = new QTextEdit(parent);
    // qDebug()<<index.row()<<index<<1;
    editor->setAcceptRichText(false); // 设置为true则支持富文本，可以显示html标签等，这里设置为false表示不处理富文本
    return editor;
}

void tScriptDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
    QString text = index.model()->data(index, Qt::DisplayRole).toString();
    QTextEdit *textEdit = static_cast<QTextEdit*>(editor);
    textEdit->setPlainText(text);
    // qDebug()<<index.row()<<2;
}

void tScriptDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
    bool jsonValid=false;
    QJsonDocument  scriptJson;
    QTextEdit *textEdit = static_cast<QTextEdit*>(editor);
    QString script = textEdit->toPlainText();
    model->setData(index, script, Qt::EditRole);
    // validateJson(text);
    tJson json;
    if(!json.validateJson(script))
    {
        jsonValid = true;
    }
    else
    {
        scriptJson = QJsonDocument::fromJson(script.toLocal8Bit());
        if(!scriptJson.isNull())
        {
            QJsonObject obj = scriptJson.object();
            QJsonValue value = obj.value("type");
            if(!value.isString())
            {
                jsonValid = true;
            }
            value = obj.value("crlf");
            if(!value.isBool())
            {
                jsonValid = true;
            }
            value = obj.value("crlf");
            if(!value.isBool())
            {
                jsonValid = true;
            }
            value = obj.value("count");
            if(!value.isDouble())
            {
                jsonValid = true;
            }
            value = obj.value("neighbor");
            if(!value.isBool())
            {
                jsonValid = true;
            }
            value = obj.value("replace");
            if(!value.isString())
            {
                jsonValid = true;
            }
            value = obj.value("onenet");
            if(!value.isBool())
            {
                jsonValid = true;
            }
            value = obj.value("ignore");
            if(!value.isBool())
            {
                jsonValid = true;
            }
        }
        else
        {
            jsonValid = true;
        }
        // QMessageBox *messageBox = new QMessageBox;
        // messageBox->information(this,"警告","json脚本配置错误\n\
        //                               {\
        //                               \"type\":\"TEXT\",//TEXT、HEX\
        //                               \"crlf\":true,//默认加回车换行:ture、不加:false\
        //                               \"count\":10,//单次循环次数1-1000\
        //                               \"neighbor\":false,//未实现\
        //                               \"replace\":\"\",//替换字符串，A口为A,B口为B\
        //                               \"onenet\":false,//onenet时自动替换ID，未实现\
        //                               \"ignore\":false //循环发送时是否忽略，忽略:ture、不忽略:false\
        //                               }");

    }
    model->setData(index, jsonValid, Qt::UserRole+1);
}

void tScriptDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    editor->setGeometry(option.rect);
    // qDebug()<<index.row()<<4;
}

void tScriptDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QColor alternateColor;
    if (index.row() % 2 == 1) {
        // 设置交替行的背景色
        alternateColor = option.palette.color(QPalette::Base);
    }
    else
    {
        alternateColor = option.palette.color(QPalette::AlternateBase);
    }
    // btnStyle->state |= index.data(Qt::UserRole).toBool() ? QStyle::State_Sunken : QStyle::State_Raised; //设置按钮状态
    if(index.data(Qt::UserRole+1).toBool())
    {
        alternateColor = Qt::red;
    }
    // qDebug()<<"alternateColor"<<alternateColor.name();
    painter->fillRect(option.rect, alternateColor);
    QStyledItemDelegate::paint(painter, option, index);
}

